package com.qczl.pme.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qczl.core.controller.BaseController;
import com.qczl.core.model.Json;
import com.qczl.pme.pojo.Town;
import com.qczl.pme.service.TownService;

/**
 * 镇信息控制器
 * @author SongR
 *
 */
@Controller
@RequestMapping(value="/town")
public class TownController extends BaseController{

	@Resource
	TownService tService;
	
	
	/**
	 * 获取所有镇信息
	 * @param aoData
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/datatable",method={RequestMethod.GET,RequestMethod.POST})
	public String getDataTable(@RequestParam String aoData){
		aoData = aoData.replaceAll("&quot;", "\"");
		String sEcho=null,sSearch = "",sort="",type="0";
		int iDisplayStart = 0;// 起始索引
		int iDisplayLength = 0;// 每页显示行数
		try {
			JSONArray jarray = JSONArray.parseArray(aoData);
			for (int i = 0; i < jarray.size(); i++) {
				JSONObject o = jarray.getJSONObject(i);
				if (o.get("name").equals("sEcho")) {
					sEcho = o.getString("value");
				}
				if(o.get("name").equals("sSearch")){
					sSearch = o.getString("value");
				}
				if (o.get("name").equals("iDisplayStart")) {
					iDisplayStart = o.getInteger("value");
				}
				if (o.get("name").equals("iDisplayLength")) {
					iDisplayLength = o.getInteger("value");
				}
			}
		} catch (Exception e) {
			JSONObject o = JSONObject.parseObject(aoData);
			String s = o.getString("sSortDir_0");
			if(s.trim().equals("desc")){
				type="1";
			}
			sEcho = o.get("sEcho").toString();
			sSearch = o.getString("sSearch");
			iDisplayStart = o.getInteger("iDisplayStart");
			iDisplayLength = o.getInteger("iDisplayLength");
		}
		List<Town> lst = new ArrayList<Town>();
		JSONObject obj = new JSONObject();
		try {
			lst = tService.getAll(iDisplayStart, iDisplayLength, sSearch,sort,type);
			obj.put("sEcho", sEcho);
			obj.put("iTotalRecords", lst.size());//实际的行数
			obj.put("iTotalDisplayRecords",tService.count(sSearch));// 显示的行数,这个要和上面写的一样
			obj.put("aaData", lst);// 要以JSON格式返回
		} catch (Exception e) {
			logger.error(e.toString(),e);
		}
		return JSONArray.toJSONString(obj);
	}
	
	/**
	 * 获取最大排序号
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/maxsort",method={RequestMethod.GET})
	public Json getMaxSort(){
		Json j = new Json();
		try {
			j.setMsg("成功获取最大排序号！");
			Long sort = tService.getMaxSort();
			j.setData(sort);
		} catch (Exception e) {
			j.setCode(-1);
			logger.error(e.toString(),e);
			e.printStackTrace();
		}
		return j;
	}
	
	
	/**
	 * 添加或修改
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public Json saveOrUpdate(Town town){
		Json j = new Json();
		try {
			j.setMsg("保存成功！");
			if(null!=town){
				if(town.getTownid()!=null){
					
				}else{
					tService.saveOrUpdate(town);
				}
			}else{
				
			}
		} catch (Exception e) {
			j.setCode(-1);
			logger.error(e.toString(),e);
		}
		return j;
	}
	
}
